package com.woniu.controller;

import com.woniu.vo.MaintainerAddVo;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Maintainer;
import com.woniu.service.IMaintainerServices;
import com.woniu.utils.QiNiuUpload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.UUID;

/**
* 运维人员管理控制层
* */
@RestController
@RequestMapping("/maintainer")
@Api(tags = "运维员管理")
public class MaintainerController extends BaseController{
    @Resource
    private IMaintainerServices iMaintainerServices;
    //先写一个最基础的条件分页查询，
    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("条件分页搜索")
    public Result searchByPage(@PathVariable Integer currentPage,
                               @PathVariable Integer pageSize,
                               @RequestBody Maintainer maintainer){
        PageResult pageResult = iMaintainerServices.searchByPage(currentPage,pageSize,maintainer);

        return toDataResult(pageResult);
    }
    //新增的方法
    @PostMapping
    @ApiOperation("新增运维员")
    public Result add(@RequestBody MaintainerAddVo maintainerAddVo){
        return toResult(iMaintainerServices.add(maintainerAddVo));
    }
    //根据id查的方法
    @GetMapping("/{id}")
    @ApiOperation("根据id查运维员相关信息")
    public Result searchById(@PathVariable Long id){
        return toDataResult(iMaintainerServices.searchById(id));
    }
    //修改的方法
    @PutMapping
    @ApiOperation("编辑运维员信息")
    public Result modify(@RequestBody MaintainerAddVo maintainerAddVo){
        return toResult(iMaintainerServices.modify(maintainerAddVo));
    }
    //删除的方法
    @DeleteMapping("/{id}")
    @ApiOperation("逻辑删除运维员")
    public Result remove(@PathVariable Long id){
        return toResult(iMaintainerServices.removesById(id));
    }
    //禁用的方法
    @GetMapping("/stop/{id}")
    @ApiOperation("禁用运维员")
    public Result stopMaintainer(@PathVariable Long id){
        return toResult(iMaintainerServices.stopMaintainer(id));
    }
    //恢复的方法
    @GetMapping("/use/{id}")
    @ApiOperation("恢复运维员")
    public Result useMaintainer(@PathVariable Long id){
        return toResult(iMaintainerServices.useMaintainer(id));
    }
    //查出所有的街道列表
    @GetMapping("/getStreetList")
    @ApiOperation("获取街道列表")
    public Result searchAllStreet(){
        return toDataResult(iMaintainerServices.searchAllStreet());
    }

    //上传图片
    @RequestMapping("/upload")
    public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String str = UUID.randomUUID().toString();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = str + substring;
        //调用七牛
        QiNiuUpload.uploadImage(file.getBytes(), filename);
        return filename;
    }



}
